<template>
  <div class="create-or-edit">
    <el-card>
      <div slot="header">
        <el-steps :active="activeStep" finish-status="success">
          <el-step
            v-for="item in steps"
            :key="item.id"
            :title="item.title"
            @click.native="activeStep = item.id-1"></el-step>
        </el-steps>
      </div>
      <!-- 步骤对应的表单结构 -->
      <el-form :model="courseForm" label-position="right" label-width="100px">
        <!-- 基本信息区域 -->
        <div v-show="activeStep===0">
          <el-form-item label="名称">
            <el-input maxlength="50" show-word-limit v-model="courseForm.courseName"></el-input>
          </el-form-item>
          <el-form-item label="简介">
            <el-input maxlength="100" show-word-limit v-model="courseForm.brief"></el-input>
          </el-form-item>
          <el-form-item label="讲师姓名">
            <el-input maxlength="50" show-word-limit v-model="courseForm.teacherDTO.teacherName"></el-input>
          </el-form-item>
          <el-form-item label="职位">
            <el-input maxlength="50" show-word-limit v-model="courseForm.teacherDTO.position"></el-input>
          </el-form-item>
          <el-form-item label="讲师简介">
            <el-input maxlength="100" show-word-limit v-model="courseForm.teacherDTO.description"></el-input>
          </el-form-item>
          <el-form-item label="课程概述">
            <el-input
              v-model="courseForm.previewFirstField"
              style="width: 49%"
              placeholder="概述1"
              maxlength="20"
              show-word-limit></el-input>
            <el-input
              v-model="courseForm.previewSecondField"
              style="width: 49%; float: right"
              placeholder="概述2"
              maxlength="20"
              show-word-limit></el-input>
          </el-form-item>
          <!-- 课程封面 -->
          <course-image
            v-model="courseForm.courseListImg"
            title="课程封面">
          </course-image>
          <!-- 解锁封面 -->
          <course-image
            v-model="courseForm.courseImgUrl"
            title="解说封面">
          </course-image>
          <el-form-item label="课程排序">
            <el-input-number
            v-model="courseForm.sortNum"
            controls-position="right"
            @change="handleChange">
            </el-input-number>
          </el-form-item>
        </div>
        <!-- 销售信息 -->
        <div v-show="activeStep===1">
          <el-form-item label="售卖价格">
            <el-input type="Number" v-model="courseForm.discounts">
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
          <el-form-item label="商品原价">
            <el-input type="Number" v-model="courseForm.price">
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
          <el-form-item label="销量">
            <el-input type="Number" v-model="courseForm.sales">
              <template slot="append">单</template>
            </el-input>
          </el-form-item>
          <el-form-item label="活动标签">
            <el-input
              v-model="courseForm.discountsTag"
              maxlength="4"
              show-word-limit></el-input>
          </el-form-item>
        </div>
        <!-- 秒杀信息 -->
        <div v-show="activeStep===2">
          <el-form-item label="限时秒杀开关" label-width="120px">
            <el-switch
              v-model="isSeckill"
              active-color="#13ce66"
              inactive-color="#ff4949"
            ></el-switch>
          </el-form-item>
          <template v-if="isSeckill">
            <el-form-item label="开始时间">
              <el-date-picker
                v-model="courseForm.activityCourseDTO.beginTime"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间">
              <el-date-picker
                v-model="courseForm.activityCourseDTO.endTime"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="秒杀价">
              <el-input :min="0" type="Number" v-model="courseForm.activityCourseDTO.amount">
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
            <el-form-item label="秒杀库存">
              <el-input :min="0" type="Number" v-model="courseForm.activityCourseDTO.stock">
                <template slot="append">个</template>
              </el-input>
            </el-form-item>
          </template>
        </div>
        <!-- 课程详情 -->
        <div v-show="activeStep===3">
          <!-- 编辑器 -->
          <el-form-item label="课程详情">
            <text-editor v-model="courseForm.courseDescriptionMarkDown"></text-editor>
          </el-form-item>
          <!-- 设置状态为 上架 or 下架 -->
          <el-form-item label="是否上架">
            <el-switch
              v-model="courseForm.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            ></el-switch>
          </el-form-item>
        </div>
        <!-- 设置下一步按钮(最后阶段不显示按钮) -->
        <el-form-item style="float: right">
          <el-button
            v-if="activeStep!==0"
            size="small"
            type="success"
            @click="activeStep--"
          >上一步</el-button>
          <el-button
            v-if="activeStep!==steps.length - 1"
            size="small"
            type="primary"
            @click="activeStep++"
          >下一步</el-button>
          <el-button
            v-if="activeStep===steps.length-1"
            size="small"
            type="primary"
            @click="onSave"
          >保存</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { saveOrUpdateCourse, getCourseById } from '@/services/course'
import CourseImage from './course-image.vue'
import TextEditor from '@/components/TextEditor/index.vue'

export default {
  components: {
    CourseImage,
    TextEditor
  },
  name: '',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    courseId: {
      type: [Number, String]
    }
  },
  created () {
    // 检测当前是否为编辑功能，如果是，根据 courseID 请求课程数据
    if (this.isEdit) {
      this.loadCourse()
    }
  },
  data () {
    return {
      // 添加课程的相关信息
      //   - 将数据中与 ID 相关的数据去除，因为是编辑功能使用的
      courseForm: {
        // id: 0,
        // 课程名称
        courseName: '',
        // 课程介绍
        brief: '',
        teacherDTO: {
          // id: 0,
          // courseId: 0,
          // 讲师名称
          teacherName: '',
          teacherHeadPicUrl: '',
          position: '',
          // 讲师介绍
          description: ''
        },
        // 课程详情内容
        courseDescriptionMarkDown: '<h3>默认值<h3>',
        // 商品原价
        price: 0,
        // 售卖价格
        discounts: 0,
        priceTag: '',
        // 活动标签
        discountsTag: '',
        isNew: true,
        isNewDes: '',
        // 课程封面地址
        courseListImg: '',
        // 解锁封面地址
        courseImgUrl: '',
        // 课程排序
        sortNum: 0,
        // 概述1
        previewFirstField: '',
        // 概述2
        previewSecondField: '',
        // 上架状态，默认值 0 代表不上架，1 代表上架
        status: 0,
        // 销量
        sales: 0,
        // 参与秒杀活动的课程
        activityCourse: false,
        activityCourseDTO: {
          // id: 0,
          // courseId: 0,
          // 秒杀活动开始时间
          beginTime: '',
          // 结束时间
          endTime: '',
          // 秒杀活动价格
          amount: 0,
          // 秒杀库存
          stock: 0
        },
        autoOnlineTime: ''
      },
      // 步骤的进度
      activeStep: 0,
      // 步骤条的相关信息
      steps: [
        { id: 1, title: '基本信息' },
        { id: 2, title: '销售信息' },
        { id: 3, title: '秒杀信息' },
        { id: 4, title: '课程详情' }
      ],
      // 本地预览图片地址(选择图片后显示在页面上的地址)
      imageUrl: '',
      // 秒杀状态
      isSeckill: false
    }
  },
  methods: {
    // 编辑功能根据 ID 加载课程信息
    async loadCourse () {
      const { data } = await getCourseById(this.courseId)
      if (data.code === '000000') {
        // 判断当前课程是否为开启秒杀的状态，如果未开启秒杀，需要初始化
        if (!data.data.activityCourse) {
          data.data.activityCourseDTO = {
            beginTime: '',
            endTime: '',
            amount: 0,
            stock: 0
          }
        }
        this.courseForm = data.data
      }
    },
    // 保存并提交数据
    async onSave () {
      const { data } = await saveOrUpdateCourse(this.courseForm)
      if (data.code === '000000') {
        this.$message.success(`${this.isEdit ? '编辑' : '添加'}课程成功`)
        this.$router.push({
          name: 'course'
        })
      }
    },
    handleChange (val) {
      console.log(val)
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-step {
    cursor: pointer;
  }
</style>
